<?php
namespace app\common\model;

use think\Model;

class News extends Base
{
	protected $autoWriteTimestamp = true;
	/**
	 * 后台自动化分页
	 */
    public function getNews(){
        $data['status']=[
            'neq',config('code.status_delete')
        ];

        $order = ['id'=>'desc'];
        //查询
        $result = $this->where($data)
            ->order($order)
            ->paginate();
        return $result;
    }

    //总条数
    public function getNewsCount(){
        $result = $this->count();
        return $result;
    }


    /**
     * [getNewsByCondition 根据条件查询数据]
     * @param  array  $param [description]
     * @return [type]        [description]
     */
    public function getNewsByCondition($condition = [], $form = 0, $size = 5){
        if (!isset($condition)) {
            $condition['status'] = [
                'neq',config('code.status_delete'),
            ];
        }

        $order = ['id'=>'desc'];

        //limit a,b
       $result = $this->where($condition)
            ->field($this->_getListField())
            ->limit($form,$size)
            ->order($order)
            ->select();
        return $result;
    }

    /**
     * [getNewsCountCondition 满足条件的数据总数]
     * @return [type] [description]
     */
    public function getNewsCountCondition($condition = []){
        if (!isset($condition)) {
            $condition['status']=[
                'neq',config('code.status_delete'),
            ];
        }
        return $this->where($condition)->count();
    }

        /**
     * [getNewsCountCondition 满足条件的数据总数]
     * @return [type] [description]
     */
    public function editConditionData($condition = [],$data = []){
        $condition['status']=[
            'neq',config('code.status_delete')
        ];
        return $this->save($data, $condition);
    }

    /**
     * [getIndexHeadNormalNews 获取首页头图数据]
     * @param  integer $num [description]
     * @return [type]       [description]
     */
    public function getIndexHeadNormalNews($num=4){
        $data = [
            'status'=>1,
            'is_head_figure'=>1,
        ];

        $order=[
            'id'=>'desc',
        ];

        return $this->where($data)
            ->field($this->_getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }

    /**
     * [getRankNormalNews 文章排序]
     * @param  integer $num [description]
     * @return [type]       [description]
     */
    public function getRankNormalNews($num=5){
        $data = [
            'status'=>1,
        ];

        $order=[
            'read_count'=>'desc',
        ];

        return $this->where($data)
            ->field($this->_getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }


    /**
     * [getPositionNormalNews 获取推荐的数据]
     * @param  integer $num [description]
     * @return [type]       [description]
     */
    public function getPositionNormalNews($num=20){
        $data = [
            'status'=>1,
            'is_position'=>1,
        ];

        $order=[
            'id'=>'desc',
        ];

        return $this->where($data)
            ->field($this->_getListField())
            ->order($order)
            ->limit($num)
            ->select();
    }

    private function _getListField(){
        return ['id',
            'catid',
            'image',
            'title',
            'read_count',
            'status',
            'is_position',
            'update_time',
            'create_time'
        ];
    }

}
